package com.zhongwang.mms.module.buy.service;

import com.alibaba.excel.EasyExcel;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.basic.dao.DictCommonDao;
import com.zhongwang.mms.module.basic.dao.VerifyProcessMapper;
import com.zhongwang.mms.module.basic.model.DictCommon;
import com.zhongwang.mms.module.basic.model.VerifyProcess;
import com.zhongwang.mms.module.buy.dao.BPreOrderPlanDao;
import com.zhongwang.mms.module.buy.model.*;
import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.module.system.user.UserDao;
import com.zhongwang.mms.util.FileUtils;
import com.zhongwang.mms.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.zhongwang.mms.constant.Constant.FORM_STATUS_REJECTED;

@Service
public class BuyProcurementPlanService {
    @Autowired
    private BPreOrderPlanDao bPreOrderPlanDao;
    @Autowired
    private LogService logService;
    @Autowired
    private VerifyProcessMapper verifyProcessMapper;
    @Autowired
    private UserDao userDao;
    @Autowired
    private DictCommonDao dictCommonDao;
    @Value("${user.upload.path}")
    private String uploadPath;


    /**
     * 查询采购计划列表 -- 明细模式 -- 普通搜索
     *
     * @param where 搜索条件
     * @return 采购计划列表
     */
    public List<BPreOrderPlan> findProcurementPlanByQuery(BPreOrderPlan where) {
        return bPreOrderPlanDao.selectProcurementPlanByWhere(where);
    }
    /**
     * 预购明细列表 -- 明细模式 -- 普通搜索
     *
     * @param where 搜索条件
     * @return 采购计划列表
     */
    public List<BPreOrderPlan> findDetailsProcurementPlanByQuery(BPreOrderPlan where) {
        return bPreOrderPlanDao.selectDetailsProcurementPlanByQuery(where);
    }
    /**
     * 查询采购计划列表 -- 明细模式 -- 高级搜索
     *
     * @param where 搜索条件
     * @return 采购计划列表
     */
    public List<BPreOrderPlan> findProcurementPlanByQueryForAdvanceList(BPreOrderPlan where) {
        return bPreOrderPlanDao.findProcurementPlanByQueryForAdvanceList(where);
    }
    /**
     * 查询预购明细列表 -- 明细模式 - 高级搜索
     *
     * @param where 搜索条件
     * @return 采购计划列表
     */
    public List<BPreOrderPlan> findDetailsProcurementPlanByQueryForAdvanceList(BPreOrderPlan where) {
        return bPreOrderPlanDao.findDetailsProcurementPlanByQueryForAdvanceList(where);
    }
    /**
     * 查询采购计划列表 -- 明细模式
     *
     * @param where 搜索条件
     * @return 采购计划列表
     */
    public BPreOrderPlan getProcurementPlanByQueryAndTrackNumber(BPreOrderPlan where) {
        return bPreOrderPlanDao.selectProcurementPlanByWheredTrackNumber(where);
    }

    /**
     * 查询采购计划列表 -- 汇总模式
     *
     * @param where 搜索条件
     * @return 采购计划列表
     */
    public List<BPreOrderPlan> findProcurementSummaryListPlanByQuery(BPreOrderPlan where) {
        return bPreOrderPlanDao.selectProcurementSummaryListPlanByWhere(where);
    }
    /**
     * 查询采购计划列表 -- 汇总模式
     * @param where 搜索条件
     * @return 采购计划列表
     */
    public List<BPreOrderPlan> findDetailsProcurementSummaryListPlanByQuery(BPreOrderPlan where) {
        return bPreOrderPlanDao.selectDetailsProcurementSummaryListPlanByWhere(where);
    }
    /**
     * 根据预购编号code查询预购计划列表
     *
     * @param code 预购编号
     * @return 预购计划列表
     */
    public List<BPreOrderPlan> findBPreOrderPlanByCode(String code) {
        return bPreOrderPlanDao.selectListByCode(code);
    }

    /**
     * 根据主键获取对应的预购计划数据
     *
     * @param id
     * @return 对应的预购计划数据
     */
    public BPreOrderPlan getBPreOrderPlanByPrimaryKey(Integer id) {
        return bPreOrderPlanDao.selectByPrimaryKey(id);
    }

    /**
     * 驳回
     *
     * @param codes
     * @param rejectReason
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int reject(List<String> codes, String rejectReason) {
        Integer result = 0;
        for (String code : codes) {
            VerifyProcess vp = verifyProcessMapper.findCompVpBySheetCode(code);
            List<BPreOrderPlan> bPreOrderPlanList = bPreOrderPlanDao.selectListByCode(code);
            User user = userDao.findUserById(bPreOrderPlanList.get(0).getAssignPurchaserId());
            logService.saveLog("采购管理-采购计划编辑", "驳回指派给" + user.getName() + "的采购计划，预购编号为：" + vp.getSheetCode());
            for (BPreOrderPlan bPreOrderPlan : bPreOrderPlanList) {
                if(rejectReason.isEmpty()){
                    bPreOrderPlan.setRejectReason("被驳回");
                }else{
                    bPreOrderPlan.setRejectReason(rejectReason);
                }
                bPreOrderPlan.setStatus(FORM_STATUS_REJECTED);
                result += bPreOrderPlanDao.updatePreOrder(bPreOrderPlan);
            }
            //直接驳回到提交前
            result += verifyProcessMapper.delBySheetCode(code);
        }
        return result;
    }

    /**
     * 导入采购计划
     * @param bPreOrderPlanList 采购计划列表
     * @return 导入数据数量
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer saveImportProcurementPlan(List<BPreOrderPlan> bPreOrderPlanList) {
        Integer result = 0;
        for (BPreOrderPlan bPreOrderPlan : bPreOrderPlanList) {
            result+= bPreOrderPlanDao.updateProcurementPlanByTrackNumber(bPreOrderPlan);
        }
        return result;
    }
    /**
     * 根据跟踪号修改收货状态
     * @param trackNumbers 物料跟踪号集合
     * @param receiptStatus 收缩状态值
     * @return 操作数据量
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer changeReceiptStatusByTrackNumber(List<String> trackNumbers, Integer receiptStatus) {
        String logContent = "将物料跟踪号为";
        for (String trackNumber : trackNumbers) {
            logContent+="【"+trackNumber+"】，";
        }
        logContent=logContent.substring(0,logContent.length()-1);
        logContent+="的采购计划的进行批量操作，把收货状态修改为";
        if(receiptStatus == 1){
            logContent+="【未收货】";
        }
        else if(receiptStatus == 2){
            logContent+="【收货】";
        }
        else if(receiptStatus == 3){
            logContent+="【无需收货】";
        }
        logService.saveLog("采购管理-采购计划编辑", logContent);
        return bPreOrderPlanDao.updateProcurementPlanReceiptStatusByTrackNumberList(trackNumbers,receiptStatus);
    }
    /**
     * 根据物料跟踪号保存修改的外购计划数据
     * @param data 物料跟踪号和修改的外购计划数据
     * @return 保存结果数量
     */
    public Integer saveUpdate(BPreOrderPlan data) {
        logService.saveLog("采购管理-采购计划编辑","修改物料跟踪号为【"+data.getTrackNumber()+"】的采购计划！");
        return bPreOrderPlanDao.updateProcurementPlanByTrackNumber(data);
    }

    /**
     * 根据物料跟踪号获取物料跟踪号是空的采购计划数量
     * @param trackNumbers 物料跟踪号几号
     * @return 跟踪号是空的采购计划数量
     */
    public Integer getPcNoNullCountByTrackNumber(List<String> trackNumbers) {
        return bPreOrderPlanDao.getPcNoNullCountByTrackNumber(trackNumbers);
    }
    /**
     * 根据物料跟踪号集合撤销采购
     * @param trackNumbers 物料跟踪号集合
     * @return 操作结果
     */
    public Integer revocationProcurementByTrackNumber(List<String> trackNumbers) {
        String logContent = "将物料跟踪号为";
        for (String trackNumber : trackNumbers) {
            logContent+="【"+trackNumber+"】，";
        }
        logContent=logContent.substring(0,logContent.length()-1);
        logContent+="的采购计划执行撤销采购操作！";
        logService.saveLog("采购管理-采购计划编辑", logContent);
        return bPreOrderPlanDao.revocationProcurementByTrackNumber(trackNumbers);
    }
    /**
     * 根据物料跟踪号保存修改的外购计划数据的收货数量
     * @param data 物料跟踪号和修改的外购计划数据的收货数量
     * @return 保存结果
     */
    public Integer saveReceivedNum(BPreOrderPlan data) {
        BPreOrderPlan old = bPreOrderPlanDao.selectProcurementPlanByWheredTrackNumber(data);
        logService.saveLog("采购管理-采购计划编辑","修改物料跟踪号为【"+data.getTrackNumber()+"】的采购计划的收货数量从【"+old.getReceivedNum()+"】修改成【"+data.getReceivedNum()+"】！");
        return bPreOrderPlanDao.updateProcurementPlanReceivedNumByTrackNumber(data);
    }
}
